package com.sx.gongzhong.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sx.gongzhong.R;
import com.sx.gongzhong.adapter.CommentListAdapter;
import com.sx.gongzhong.model.CommentBean;
import com.sx.gongzhong.pest.ConvennicAppealReplyPest;
import com.sx.gongzhong.utils.ToastUtils;
import com.sx.gongzhong.view.ConvennicAppealReplyView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by admin on 2018/1/4.
 */

public class ConvennicAppealComFragment extends LazyFragment implements ConvennicAppealReplyView {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private int page = 1,rows = 15;
    private String commentId;
    private CommentListAdapter commentListAdapter;
    private List<CommentBean> mlist=new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_convennice_appeal,container,false);
        ButterKnife.bind(this,view);
        initView();
        commentId = getArguments().getString("replyId","");
        getCommentData();
        return view;
    }

    private void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL,false));
        commentListAdapter = new CommentListAdapter(getActivity(),R.layout.item_commont_list,mlist);
        commentListAdapter.setColumnType("convenient_appeal_comment");
        commentListAdapter.setEmptyView(View.inflate(getActivity(),R.layout.no_comment_view,null));
        commentListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                getCommentData();
            }
        },recyclerView);
        recyclerView.setAdapter(commentListAdapter);
    }

    private void getCommentData() {
        new ConvennicAppealReplyPest(this).getAppealCommentList(commentId,page,rows);
    }

    public void onRefresh() {
        page = 1;
        getCommentData();
    }

    @Override
    protected void lazyLoad() {
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void showError(String message) {
        ToastUtils.showToast(getActivity(),message);
    }

    @Override
    public void getAppealReplyListSuccess(List<CommentBean> rows, int total) {
        if (page == 1) {
            mlist.clear();
            commentListAdapter.setNewData(mlist);
        }
        if (rows != null && rows.size() > 0) {
            mlist.addAll(rows);
        }
        if (page == total){
            commentListAdapter.loadMoreEnd();
        }else {
            commentListAdapter.loadMoreComplete();
        }
        commentListAdapter.notifyDataSetChanged();
    }
}
